//=============================================================================
// Smart Gun Inventory class
//=============================================================================
class SmartAssaultRifle extends KFWeapon
	config(user);
	
#exec OBJ LOAD FILE=AKFFonts.utx
#exec obj load file="AliensKFWeapons_T.utx"
#exec obj load file="AliensKFWeapons_A.ukx"
#exec obj load file="BDFonts.utx"
#exec obj load file="AliensKFWeapons_SM.usx"

// Free-Aim
var()		float			FreeAimRotationSpeed;		// How fast the free-aim rotation speed should be for this weapon


var() ScriptedTexture MyScriptedTexture;
var() Shader AmmoCounter;
var string MyMessage;
var int MyHealth;
var   Font MyFont;
var   Font MyFont2;
var   Font SmallMyFont;
var  color MyFontColor;
var  color MyFontColor2;
var int OldValue;

var     float               ForceZoomOutTime;

// Don't use alt fire to toggle
simulated function AltFire(float F){}
// Don't switch fire mode
exec function SwitchModes(){}

simulated function WeaponTick(float dt)
{
    super.WeaponTick(dt);

    if( ForceZoomOutTime > 0 )
    {
        if( bAimingRifle )
        {
    	    if( Level.TimeSeconds - ForceZoomOutTime > 0 )
    	    {
                ForceZoomOutTime = 0;

            	ZoomOut(false);

            	if( Role < ROLE_Authority)
        			ServerZoomOut(false);
    		}
		}
		else
		{
            ForceZoomOutTime = 0;
		}
	}
}

// Force the weapon out of iron sights shortly after firing the 203 grenade
simulated function bool StartFire(int Mode)
{
    if( super.StartFire(Mode) )
    {
        if( mode == 1 )
        {
            ForceZoomOutTime = Level.TimeSeconds + 0.4;
        }
        return true;
    }

    return false;
}

simulated function bool CanZoomNow()
{
    return ( !FireMode[1].bIsFiring &&
           ((FireMode[1].NextFireTime - FireMode[1].FireRate * 0.2) < Level.TimeSeconds + FireMode[1].PreFireTime));
}

function bool AllowReload()
{
	if( (FireMode[1].NextFireTime - FireMode[1].FireRate * 0.1) > Level.TimeSeconds + FireMode[1].PreFireTime )
	{
		return false;
	}

	return super.AllowReload();
}

simulated function bool ReadyToFire(int Mode)
{
    // Don't allow firing while reloading the shell
	if( (FireMode[1].NextFireTime - FireMode[1].FireRate * 0.06) > Level.TimeSeconds + FireMode[1].PreFireTime )
	{
		return false;
	}

    return super.ReadyToFire(Mode);
}


simulated final function SetTextColor( byte R, byte G, byte B )
{
	MyFontColor.R = R;
	MyFontColor.G = G;
	MyFontColor.B = B;
	MyFontColor.A = 255;


}

simulated final function SetTextColor2( byte R, byte G, byte B )
{
	MyFontColor2.R = R;
	MyFontColor2.G = G;
	MyFontColor2.B = B;
	MyFontColor2.A = 255;

 }


//=============================================================================
// Rendering
//=============================================================================
simulated event RenderOverlays( Canvas Canvas )
{


	local int m;
    local rotator RollMod;
    local AKFPlayer Playa;
	//For lean - Justin
	local KFHumanPawn rpawn;
	local int leanangle;
	// Drawpos actor
	local rotator RotOffset;

    if (Instigator == none)
    	return;



    // Lets avoid having to do multiple casts every tick - Ramm
    Playa = AKFPlayer(Instigator.Controller);

    // Don't draw the weapon if we're not viewing our own pawn
    if (Playa != none && Playa.ViewTarget != Instigator)
    	return;

    // draw muzzleflashes/smoke for all fire modes so idle state won't
    // cause emitters to just disappear
	Canvas.DrawActor(None, false, true); // amb: Clear the z-buffer here

    for (m = 0; m < NUM_FIRE_MODES; m++)
    {
    	if (FireMode[m] != None)
        {
        	FireMode[m].DrawMuzzleFlash(Canvas);
        }
    }
 
	// these seem to set the current position and rotation of the weapon
	// in relation to the player

	//Adjust weapon position for lean
	rpawn = KFHumanPawn(Instigator);
	if (rpawn != none )
	{
		leanangle += 100;
	}


	SetLocation( Instigator.Location + Instigator.CalcDrawOffset(self) );

	if( bUsesFreeAim )
	{
    	// Remove the roll component so the weapon doesn't tilt with the terrain
  	RollMod = Instigator.GetViewRotation();

    	if( Playa != none )
		{
			RollMod.Pitch += Playa.WeaponBufferRotation.Pitch;
			RollMod.Yaw += Playa.WeaponBufferRotation.Yaw;

			RotOffset.Pitch -= Playa.WeaponBufferRotation.Pitch;
			RotOffset.Yaw -= Playa.WeaponBufferRotation.Yaw;
   		}

		RollMod.Roll += leanangle;
	    SetRotation( RollMod );
		bDrawingFirstPerson = true;
	    Canvas.DrawActor(self, false, false, DisplayFOV);
	bDrawingFirstPerson = false;


   	}

   	RollMod = Instigator.GetViewRotation();
		RollMod.Roll += leanangle;

	// use the special actor drawing when in ironsights to avoid the ironsight "jitter"
	// TODO: This messes up the lighting, and texture environment map shader when using
	// ironsights. Maybe use a texrotator to simulate the texture environment map, or
	// just find a way to fix the problems.

//	    SetRotation( RollMod );
//		bDrawingFirstPerson = true;
//	    Canvas.DrawActor(self, false, false, DisplayFOV);


//	super(ROweapon).RenderOverlays( Canvas );



}

function SetServerOrientation(rotator NewRotation)
{
	local rotator WeaponRotation;

	if( bUsesFreeAim  )
	{
    	// Remove the roll component so the weapon doesn't tilt with the terrain
    	WeaponRotation = Instigator.GetViewRotation();// + FARotation;

		WeaponRotation.Pitch += NewRotation.Pitch;
		WeaponRotation.Yaw += NewRotation.Yaw;
		       WeaponRotation.Roll += 100;        

    	SetRotation( WeaponRotation );
		SetLocation( Instigator.Location + Instigator.CalcDrawOffset(self) );
    }
}

// Get the coords for the muzzle bone. Used for free-aim projectile spawning
function coords GetMuzzleCoords()
{
	// have to update the location of the weapon before getting the coords
	SetLocation( Instigator.Location + Instigator.CalcDrawOffset(self) );
	return GetBoneCoords('tip');
}

// returns true if this weapon should use free-aim in this particular state
simulated function bool ShouldUseFreeAim()
{


	if(bUsesFreeAim )
	{
		return true;
	}

	return false;
}

/*
simulated function RenderOverlays( Canvas Canvas )
{


    local float fHeight;
    local float fWidth;
    local float fwidth2;
    local float fheight2;
    local float fX;
    local float fY;
    local float fy2;
    local float fx2;



    fX = Canvas.ClipX * 0.725;
    fY = Canvas.ClipY * 0.940;
    fX2 = Canvas.ClipX * 0.725;
    fY2 = Canvas.ClipY * 0.935;
    fWidth = Canvas.ClipX * 0.0500000;
    fHeight = Canvas.ClipY * 0.03500000;
    fwidth2 = canvas.clipx * 0.05000000;
    fHeight2 = canvas.clipy * 0.0475000;

    //-----------------------------------------------------------

      Canvas.SetPos(fX2, fY2);
//    Canvas.Style = ERenderStyle.STY_Masked;		  
/*    Canvas.DrawTile(Texture'KillingFloorHud.HUD.Hud_BOX_128x64', fWidth2, fHeight2, 0, 0, 128, 64);   


    if(AmmoAmount(1) == 1)
    {
	Canvas.SetPos(fX, fY);		  
        Canvas.DrawTile(SecAmmo1, fWidth, fHeight, 0, 0, 128, 64);
    }	  

	    if(AmmoAmount(1) == 2)
    {
  	Canvas.SetPos(fX, fY);		  
    	Canvas.DrawTile(SecAmmo2, fWidth, fHeight, 0, 0, 128, 64);
    }	 
	
	    if(AmmoAmount(1) >= 3)
    {
	Canvas.SetPos(fX, fY);		  
        Canvas.DrawTile(SecAmmo3, fWidth, fHeight, 0, 0, 128, 64);
    }	 
        

		  
 	if( AmmoAmount(0) <= 0 )
	{
		if( OldValue!=-5 )
		{
			OldValue = -5;
	//		Skins[5] = ScopeRed;
			MyFont = SmallMyFont;
			SetTextColor(218,18,18);
			MyMessage = "Empty";
			++MyScriptedTexture.Revision;
		}
	}
	else if( bIsReloading )
	{
		if( OldValue!=-4 )
		{
			OldValue = -4;
			MyFont = SmallMyFont;
			SetTextColor(32,187,112);
			MyMessage = "RL";
			++MyScriptedTexture.Revision;
		}
	}
	else if( OldValue!=(MagAmmoRemaining+1) )
	{
		OldValue = MagAmmoRemaining+1;
//		Skins[5] = ScopeGreen;
		MyFont = Default.MyFont;

		if ((MagAmmoRemaining ) <= (MagCapacity/2))
			SetTextColor(32,187,112);
		if ((MagAmmoRemaining ) <= (MagCapacity/3))
		{
			SetTextColor(218,18,18);
//			Skins[5] = ScopeRed;
		}
		if ((MagAmmoRemaining ) >= (MagCapacity/2))
			SetTextColor(76,148,177);
		MyMessage = String(MagAmmoRemaining);

		++MyScriptedTexture.Revision;
	}

	MyScriptedTexture.Client = Self;
	//Skins[5] = AmmoCounter;
	Super.RenderOverlays(Canvas);
	MyScriptedTexture.Client = None;
	//Skins[5] = None;
}

*/
simulated function RenderTexture( ScriptedTexture Tex )


{
	local int w, h;
/*
	// Ammo
	Tex.TextSize( MyMessage, MyFont,  w, h );
	Tex.DrawText( ( Tex.USize / 2 ) - ( w / 2 ), ( Tex.VSize / 2 ) - ( h / 1.2 ),MyMessage, MyFont, MyFontColor );

*/

	Tex.TextSize( MyMessage, MyFont2,  w, h );	
//	Tex.TextSize(AmmoAmount(1), MyFont2,w,h); 
	Tex.DrawText( ( Tex.USize / 2 ) - ( w / 2.2 ), ( Tex.VSize / 2 ) - ( h / 2.0 ),/*AmmoAmount(1)*/MyMessage, MyFont, MyFontColor2 );
}
*/

simulated function BringUp(optional Weapon PrevWeapon)
{
	HandleSleeveSwapping();

	if( AKFPlayer(Instigator.Controller) != none )
	{
     	AKFPlayer(Instigator.Controller).FAAWeaponRotationFactor = FreeAimRotationSpeed;
    }
    super.bringup(prevweapon);
}

defaultproperties
{

    bCanSway = True
    bUsesFreeAim=true
	FreeAimRotationSpeed=8.0    
	 bHasSecondaryAmmo=True
	 bReduceMagAmmoOnSecondaryFire=False
	
     MyScriptedTexture=ScriptedTexture'AliensKFWeapons_T.AmmoText'
     MyFont=Font'BDFonts.DigitalMed'
     MyFont2=Font'BDFonts.DigitalMed'
     SmallMyFont=Font'BDFonts.DigitalMed'
     MyFontColor=(B=177,G=148,R=76,A=255)
     MyFontColor2=(B=18,G=18,R=218,A=255)
     //SecAmmo1=Texture'AliensKFWeapons_T.HUD.HUD_M41ANADE1'
     //SecAmmo2=Texture'AliensKFWeapons_T.HUD.HUD_M41ANADE2'
     //SecAmmo3=Texture'AliensKFWeapons_T.HUD.HUD_M41ANADE3'
     MagCapacity=99
    ReloadRate=2.000000
    ReloadAnim="Reload"
     ReloadAnimRate=1.500000
    WeaponReloadAnim="Reload_SCAR"
     HudImage=Texture'AliensKFWeapons_T.HUD.HUD_M41A_unselected'
    SelectedHudImage=Texture'AliensKFWeapons_T.HUD.HUD_M41A_selected'
     Weight=6.000000
   bHasAimingMode=True
     IdleAimAnim="Idle_Iron"
    StandardDisplayFOV=55.000000
    bModeZeroCanDryFire=True
     TraderInfoTexture=Texture'AliensKFWeapons_T.Trader.Trader_M41A'
     PlayerIronSightFOV=70.000000
     ZoomedDisplayFOV=45.000000
     PutDownAnim="PutDown"
     SelectSound=Sound'AliensKFWeapons_S.Select'
     SelectForce="SwitchToAssaultRifle"
     AIRating=0.550000
     CurrentRating=0.550000
     bShowChargingBar=True
    AmmoClass(0)=Class'AliensKFWeapons.M41AAmmo'
     AmmoClass(1)=Class'AliensKFWeapons.M41AProjectileAmmo'
     Description="The M41A Pulse Rifle from the movie Aliens."
     EffectOffset=(X=100.000000,Y=25.000000,Z=-10.000000)
     DisplayFOV=55.000000
     Priority=15
     SleeveNum=1
     CustomCrosshair=11
     CustomCrossHairTextureName="Crosshairs.HUD.Crosshair_Cross5"
     InventoryGroup=4
     GroupOffset=7
     PlayerViewOffset=(X=20.000000,Y=19.000000,Z=3.000000)
     BobDamping=6.000000
      IconCoords=(X1=245,Y1=39,X2=329,Y2=79)
     ItemName="M41A"
     Mesh=SkeletalMesh'AliensKFWeapons_A.M41APulseRifle'
     Skins(0)=Combiner'AliensKFWeapons_T.M41A_cmb'
     TransientSoundVolume=5.250000
     FireModeClass(0)=Class'AliensKFWeapons.smartFire'
     FireModeClass(1)=Class'AliensKFWeapons.M41AALTFire'

     PickupClass=Class'AliensKFWeapons.SmartPickup'

     AttachmentClass=Class'AliensKFWeapons.SmartAttachment'

}
